const db = require('../db')
exports.mNr = (req, res) => {
    let caricatureId = req.body.caricatureId
    if (caricatureId == null) {
        return res.cc('请添加 caricatureId')
    }
    const sqlStr = `SELECT * FROM nr where caricatureId = ? `
    db.query(sqlStr, caricatureId, (err, results) => {
        if (err) return res.cc(err)
        if (results.length != 1) return res.cc('没有此漫画')
        res.send(results)
    })
}

exports.allM = (req, res) => {
    let tuijian = req.body.tuijian
    let sqlStr = ''
    if (tuijian == 'tuijian') {
        sqlStr = ` SELECT * FROM allmh LIMIT 0,6 `//查找六条数据
    } else if (req.body.caricatureId != undefined) {
        sqlStr = ` SELECT * FROM allmh where caricatureId = '${req.body.caricatureId}'`//查找指定的数据
    } else if (req.body.find == 'find') {
        sqlStr = ` SELECT * FROM allmh WHERE name like '%${req.body.qc}%';`
    }
    else {
        sqlStr = ` SELECT * FROM allmh  `//分类查找
        let index = 1
        for (p in req.body) {
            if (req.body[p] == 000) {
                continue
            } else if (index == 1) {
                sqlStr += `where ${p}= '${req.body[p]}'`
                index++
            } else {
                sqlStr += ` and ${p}= '${req.body[p]}'`
            }
        }
    }
    db.query(sqlStr, (err, results) => {
        if (err) return res.cc(err)
        if (results.length == 0) return res.cc('没有此漫画')
        return res.send(results)
    })
}

exports.manhua = (req, res) => {
    const sqlStr = `SELECT * FROM ${req.body.caricatureId} where hua= ${req.body.hua}`
    db.query(sqlStr, (err, results) => {
        if (err) return res.cc(err)
        if (results.length == 0) return res.cc('没有此漫画')
        return res.send(results)
    })
}